package com.heima.file.config;

import com.heima.file.service.FileStorageService;
import com.heima.file.service.impl.MinIOFileStorageService;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;


@Configuration
@EnableConfigurationProperties(MinIOConfigProperties.class)
public class MinIOAutoConfiguration {
    @Resource
    private MinIOConfigProperties minIOConfigProperties;

    @Bean
    @ConditionalOnMissingBean
    public MinioClient buildMinioClient() {
        return MinioClient
                .builder()
                .credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey())
                .endpoint(minIOConfigProperties.getEndpoint())
                .build();
    }

    @Bean
    @ConditionalOnMissingBean
    public FileStorageService fileStorageService(MinioClient minioClient) {
        return new MinIOFileStorageService(minioClient, this.minIOConfigProperties);
    }
}
